import {INPUT_CHANGE, ADD_ITEM, DELETE_ITEM, GET_LIST} from './actionTypes'

let initState = {
    defaultValue: '123',
    dataList: []
}
export default (state = initState, action) => {
    console.log('action', action)
    if(action.type === INPUT_CHANGE){
        //reducer里可以改变state但不这么做，通常只接受state不改变state，由store改变state
        /* state.defaultValue = action.value
        return state */
        //考虑库的效率不会进行深层对比，因此通深拷贝state使redux进行刷新
        let newState = JSON.parse(JSON.stringify(state))
        newState.defaultValue = action.value
        return newState
    }
    if(action.type === ADD_ITEM){
        if(state.defaultValue){
            let newState = JSON.parse(JSON.stringify(state))
            newState.dataList.push({name: state.defaultValue, age: 23})
            newState.defaultValue = ''
            return newState
        }
    }
    if(action.type === DELETE_ITEM){
        let newState = JSON.parse(JSON.stringify(state))
        newState.dataList.splice(action.index, 1)
        return newState
    }
    if(action.type === GET_LIST){
        let newState = JSON.parse(JSON.stringify(state))
        newState.dataList = action.data
        return newState
    }
    return state
}